//
//  BusinessCaseItemListViewController.m
//  HAODE
//
//  Created by panquanReabam.com on 15/11/5.
//  Copyright © 2015年 reabam.com. All rights reserved.
//

#import "BusinessCaseItemListViewController.h"
#import "HAODEsingleton.h"
#import "UIViewExt.h"
#import "HttpRequest.h"
#import "JSONKit.h"

#import "shopcatCell.h"
#import "RDVTabBarController.h"
#import "MJRefresh.h"
#import "UIButton+WebCache.h"
#import "proview.h"
#import "GetProductListViewController.h"
#import "TheLoginViewController.h"
#import "GetProductDetailViewController.h"
@interface BusinessCaseItemListViewController ()<UIGestureRecognizerDelegate,UINavigationControllerDelegate,UITextFieldDelegate,UISearchBarDelegate,UIScrollViewDelegate>

@end

@implementation BusinessCaseItemListViewController

{UIScrollView*mainScroll;
    HAODEsingleton*commdity;

}
-(void)BackAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
    
}
- (void)viewDidDisappear:(BOOL)animated

{
    
    [super viewDidDisappear:animated];
    
    
    
    self.navigationController.delegate = nil;
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [[self rdv_tabBarController] setTabBarHidden:YES];
    
        self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.navigationController.delegate = self;
    
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    
    
    self.title=@"关联商品";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor blackColor],
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
    
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];

    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    commdity=[HAODEsingleton shared];
    [self jiazaishuju];
    
}
-(void)jiazaishuju{
    
   
    
    mainScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0, ScreenWidth,ScreenHeight)];
    mainScroll.delegate = self;
    mainScroll.userInteractionEnabled = YES;
    mainScroll.scrollEnabled = YES;
    mainScroll.showsVerticalScrollIndicator = FALSE;
    mainScroll.showsHorizontalScrollIndicator = FALSE;
    [self.view addSubview:mainScroll];
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* tokenId = [user objectForKey:@"tokenId"];
    
    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setValue:tokenId forKey:@"tokenId"];
    
     [params setValue:commdity.caseID forKey:@"caseID"];
    [HttpRequest requestData:@"Business/Case/ItemList" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            NSLog(@"====%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                
          
                
                
                NSArray *product = [severDataDict.dataDict objectForKey:@"DataLine"];
                
                if (product.count>0) {
                    if (![product isEqual:[NSNull null]]) {
                        
                        for (int i = 0,j = -1; i < product.count; i++) {
                            if (i%2 == 0) {
                                j++;
                            }
                            proview * clothView = [[proview alloc]initWithFrame:CGRectMake(10+(ScreenWidth/2-5)*(i%2), 10+260*j, ScreenWidth/2-15, 230)];
                            
                            clothView.label_name.text =[NSString stringWithFormat:@"%@ %@", [[product objectAtIndex:i] objectForKey:@"itemName"],[[product objectAtIndex:i] objectForKey:@"itemType"]];
                            NSString*GoldPrice=[NSString stringWithFormat:@"%@",[[product objectAtIndex:i] objectForKey:@"salePrice"]];
                            NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
                            [numberFormatter setPositiveFormat:@"###,##0;"];
                            NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:GoldPrice.floatValue]];
                            clothView.label_price.text =[NSString stringWithFormat:@"¥ %@",formattedNumberString ];
                            clothView.saleQty.text =[NSString stringWithFormat:@" 销量 %@ 件", [[product objectAtIndex:i] objectForKey:@"saleQty"]];
                            [clothView.buttonView setImageWithURL:[NSURL URLWithString:[[product objectAtIndex:i] objectForKey:@"imageUrlFull"]] placeholderImage:nil];
                            
                            [clothView.buttonView addTarget:self action:@selector(shangping:) forControlEvents:UIControlEventTouchUpInside];
                            
                            [clothView.buttonView setTitle:[[product objectAtIndex:i] objectForKey:@"itemId"] forState:UIControlStateNormal];
                            [mainScroll addSubview:clothView];
                            
                        }
                        
                        
                        mainScroll.contentSize = CGSizeMake(ScreenWidth, 260*(product.count/2+product.count%2)+50);
                    }
                    
                }else{
                    
                    mainScroll.frame=CGRectMake(0, 10, ScreenWidth, ScreenHeight);
                    
                }
                
                
                
                
                    
                    
              
            }
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            
            [Global showTextHudSub:@"连接失败" view:self.view];
            
            
            
        }
        
    }];
    
    
    
}

-(void)shangping:(UIButton*)sender{
    
    
    [self sea1:[NSString stringWithFormat:@"%@",sender.titleLabel.text]];
    
}

- (void)sea1:(NSString *)String{
    //[self.data startAnimating];
    
    
    commdity.itemId=String;
    
    GetProductDetailViewController*vc1=[[GetProductDetailViewController alloc] init];
    
    [self.navigationController pushViewController:vc1 animated:YES];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
